#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use screenshots::Screen as winscreen;
use pixel_caster::{*};
#[tauri::command] 
fn screenshots(left:i32,top:i32,width:u32,height:u32,screenx:i32,screeny:i32,screenwidth:u32,screenheight:u32)->Vec<u8>{
    let screens = winscreen::all().unwrap();
    let mut buffer:Vec<u8>=Vec::new();
    for screen in screens {
        if screenx == screen.display_info.x && screeny == screen.display_info.y && screenwidth == screen.display_info.width && screenheight == screen.display_info.height {
            let image = screen.capture_area(left,top,width,height).unwrap();
            buffer = image.buffer().to_vec();
        }
    }
    return buffer;
}

#[tauri::command]
fn getcolor(left:i32,top:i32,width:u32,height:u32)->Vec<u8>{
    let mut screen_u8 :Screen<u8> = Screen::new(left,top,width,height);
    screen_u8.scan_area();
    let bgra = screen_u8.get_bytes();
    return bgra.to_vec();
}


use tauri_plugin_clipboard;
use tauri::{SystemTrayEvent};
use tauri::Manager;
fn main() {
    tauri::Builder::default()
        .system_tray(systemtray())
        .on_system_tray_event(|app,event| match event {
            SystemTrayEvent::LeftClick {
                position: _,
                size: _,
                ..
              } => {
                let window = app.get_window("main").unwrap();
                window.show().unwrap();
                window.set_focus().unwrap();
              }
              SystemTrayEvent::RightClick {
                position: _,
                size: _,
                ..
              } => {
                println!("system tray received a right click");
              }
              SystemTrayEvent::DoubleClick {
                position: _,
                size: _,
                ..
              } => {
                let window = app.get_window("main").unwrap();
                window.show().unwrap();
                window.set_focus().unwrap();
              }
              SystemTrayEvent::MenuItemClick { id, .. } => {
                match id.as_str() {
                  "quit" => {
                    std::process::exit(0);
                  }
                  "hide" => {
                    let window = app.get_window("main").unwrap();
                    window.hide().unwrap();
                  }
                  "open"=> {
                    let window = app.get_window("main").unwrap();
                    window.show().unwrap();
                    window.set_focus().unwrap();
                  }
                  "cliboard"=>{
                    let window = app.get_window("cliboard").unwrap();
                    window.show().unwrap();
                    window.set_focus().unwrap();
                  }
                  _ =>{}
                }
              }
              _ => {}
        })
        .plugin(tauri_plugin_clipboard::init())
        .invoke_handler(tauri::generate_handler![screenshots,getcolor])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}


// system tray 系统托盘菜单
use tauri::SystemTray;
use tauri::{CustomMenuItem, SystemTrayMenu, SystemTrayMenuItem};
fn systemtray()->SystemTray{
    // 创建菜单项
    let quit = CustomMenuItem::new("quit".to_string(), "退出");
    let hide = CustomMenuItem::new("hide".to_string(), "隐藏");
    let open = CustomMenuItem::new("open".to_string(), "打开");
    let cliboard = CustomMenuItem::new("cliboard".to_string(), "剪贴板");

    // 挂载菜单项到系统托盘菜单
    let tray_menu = SystemTrayMenu::new()
    .add_item(quit)
    .add_native_item(SystemTrayMenuItem::Separator)
    .add_item(hide)
    .add_native_item(SystemTrayMenuItem::Separator)
    .add_item(open)
    .add_native_item(SystemTrayMenuItem::Separator)
    .add_item(cliboard);

    // 挂在系统托盘菜单到系统托盘
    let tray = SystemTray::new().with_menu(tray_menu);
    return tray;
}

